﻿/**************************************************************************
创建日期:	2019/6/19 15:59:38     
作	  者:	张存
邮 	  箱:	zhangcunliang@126.com
创建时间:	
Copyright (c)  ZhCun.cn

描	述： 参考文档：https://www.cnblogs.com/intotf/p/10191669.html
        其他更详细的参考：https://www.cnblogs.com/lwqlun/p/10954936.html
记	录：
***************************************************************************/
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;

namespace ZhCun.WebCommon
{
    /// <summary>
    /// 定义可读Body的接口
    /// </summary>
    public interface IReadableBody
    {
        /// <summary>
        /// 获取或设置是否可读
        /// </summary>
        bool IsRead { get; set; }

        /// <summary>
        /// 读取文本内容
        /// </summary>
        /// <returns></returns>
        Task<string> ReadAsStringAsync();
    }

    /// <summary>
    /// 可读的Response Body
    /// </summary>
    public class ReadableResponseBody : MemoryStream, IReadableBody
    {
        /// <summary>
        /// 流内容
        /// </summary>
        private readonly Stream body;

        /// <summary>
        /// 获取或设置是否可读
        /// </summary>
        public bool IsRead { get; set; }

        /// <summary>
        /// 构造自定义流
        /// </summary>
        /// <param name="body"></param>
        public ReadableResponseBody(Stream body)
        {
            this.body = body;
        }
        
        /// <summary>
        /// 写入响应流
        /// </summary>
        /// <param name="buffer"></param>
        /// <param name="offset"></param>
        /// <param name="count"></param>
        public override void Write(byte[] buffer, int offset, int count)
        {
            //BodyBuffer = new byte[count];
            //buffer.CopyTo(BodyBuffer, offset);
            base.Write(buffer, offset, count);            
            //this.body.Write(buffer, offset, count);
            //if (this.IsRead)
            //{
            //    base.Write(buffer, offset, count);
            //}
        }

        public byte[] BodyBuffer { private set; get; }

        private int BufferOffset;
        private int BufferCount;

        public string ReadBufferString()
        {
            var bodyStr = System.Text.Encoding.UTF8.GetString(BodyBuffer, 0, BodyBuffer.Length);
            //BodyBuffer = null;
            return bodyStr;
        }

        /// <summary>
        /// 读取响应内容
        /// </summary>
        /// <returns></returns>
        public Task<string> ReadAsStringAsync()
        {
            if (this.IsRead == false)
            {
                throw new NotSupportedException();
            }

            this.Seek(0, SeekOrigin.Begin);
            using (var reader = new StreamReader(this))
            {
                return reader.ReadToEndAsync();
            }
        }
        ///// <summary>
        ///// 写入响应流
        ///// </summary>
        ///// <param name="source"></param>
        //public override void Write(ReadOnlySpan<byte> source)
        //{
        //    //this.body.Write(source);
        //    this.body.Write(source);
        //    if (this.IsRead)
        //    {
        //        base.Write(source);
        //    }
        //}

        /// <summary>
        /// 刷新响应流
        /// </summary>
        public override void Flush()
        {
            this.body.Flush();

            if (this.IsRead)
            {
                base.Flush();
            }
        }

        

        protected override void Dispose(bool disposing)
        {
            this.body.Dispose();
            base.Dispose(disposing);
        }
    }
}